package com.hjx.windowtool.cmd;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;

import javax.swing.JScrollPane;
import javax.swing.JList;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.awt.event.ActionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class WindowsListDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JList list;
	private String result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			WindowsListDialog dialog = new WindowsListDialog();
			dialog.open();
			System.out.println("12321312");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void open() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	public String getResult() {
		return result;
	}
	/**
	 * Create the dialog.
	 */
	public WindowsListDialog() {
		setModal(true);
		setBounds(100, 100, 450, 300);
		setAlwaysOnTop(true);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		contentPanel.add(scrollPane, BorderLayout.CENTER);
		
		list = new JList();
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				System.out.println(list.getSelectedValue());
				result=list.getSelectedValue().toString();
			}
		});
		list.setModel(model);
		scrollPane.setViewportView(list);

		JPanel buttonPane = new JPanel();
		FlowLayout fl_buttonPane = new FlowLayout(FlowLayout.RIGHT);
		fl_buttonPane.setHgap(15);
		buttonPane.setLayout(fl_buttonPane);
		getContentPane().add(buttonPane, BorderLayout.SOUTH);

		JButton okButton = new JButton("确认");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);

		JButton cancelButton = new JButton("取消");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				result=null;
				dispose();
			}
		});
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);
		
		JLabel label = new JLabel("查找");
		panel.add(label);
		
		textField = new JTextField();
		panel.add(textField);
		textField.setColumns(10);
		
		JButton button = new JButton("查找");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = textField.getText();
				if(text==null||text.trim().isEmpty()) {
					return;
				}
				model.filter(new Predicate<String>() {
					@Override
					public boolean test(String t) {
						return t.contains(text);
					}
				});
			}
		});
		panel.add(button);
		init();
	}
	private List<String> listObjects=new ArrayList<>();
	private StringListModel model=new StringListModel();
	private JTextField textField;
	private void init() {
		HWND findWindowEx = User32.INSTANCE.FindWindowEx(null, null, null, null);
		System.out.println(findWindowEx);
		while(findWindowEx!=null) {
			int getWindowTextLength = User32.INSTANCE.GetWindowTextLength(findWindowEx);
			char[] c=new char[getWindowTextLength+1];
			User32.INSTANCE.GetWindowText(findWindowEx, c, c.length);
			String x = Pointer.nativeValue(findWindowEx.getPointer())+"-"+new String(c).trim();
			System.out.println(x);
			listObjects.add(x);
			findWindowEx = User32.INSTANCE.FindWindowEx(null, findWindowEx, null, null);
			System.out.println(findWindowEx);
		}
		model.setListSource(listObjects);
		model.refresh();
	}

}
